import time
import unittest
import requests
import json
#测试登录模块
from parameterized import parameterized


# 构建测试数据
from jxc.tpshop.api.login import LoginAPI


# 构造测试数据
from jxc.tpshop.tools import dbutil


def build_data():
    sql = "select * from t_login"
    db_data = dbutil.exe_sql(sql)
    print(db_data)
    test_data = []
    for case_data in db_data:
        username = case_data[2]
        password = case_data[3]
        verify_code = case_data[4]
        status_code = case_data[5]
        content_type = case_data[6]
        status = case_data[7]
        msg = case_data[8]
        test_data.append((username, password, verify_code, status_code,
        content_type, status, msg))
        print("test_data = {}".format((username, password, verify_code,
        status_code, content_type, status, msg)))
        return test_data


class TestLoginParms(unittest.TestCase):
    #每一个test开始都会先执行
    def setUp(self):
        self.login_api=LoginAPI()
        # 实例化session对象
        self.session=requests.Session()

    def tearDown(self):
        if self.session:
            self.session.close()

    @parameterized.expand(build_data)
    def test01_login_success(self,login_data, status_code, code, message):
        #验证码接口
        response=self.login_api.get_verify_code(self.session)
        print("type=", response.headers.get("Content-type"))
        self.assertIn("image",response.headers.get("Content-type"))
        self.assertEqual(200,response.status_code)

        # 调用登录接口进行登录
        response=self.login_api.login(self.session,login_data)
        json_data=response.json()
        print("login response data1=",json_data)
        #断言
        self.assertEqual(200,response.status_code)
        self.assertEqual(0,json_data.get("code"))
        self.assertEqual("操作成功",json_data.get("msg"))


    # def test02_user_is_not_exist(self):
    #     #获取验证码
    #     response=self.session.get(self.verify_url)
    #     print("type=",response.headers.get("Content-type"))
    #     self.assertIn("image",response.headers.get("Content-type"))
    #     self.assertEqual(200,response.status_code)
    #
    #     data = {"username": "jacke",
    #                 "password": "xjt123",
    #                 "verifyCode": "9999"}
    #
    #     response=self.session.post(self.login_url,data=data)
    #     json_data=response.json()
    #     print("login response data1=",json_data)
    #     #断言
    #     self.assertEqual(200,response.status_code)
    #     self.assertEqual(5901,json_data.get("code"))
    #     self.assertEqual("用户名或密码输入有误",json_data.get("msg"))
    #
    # def test03_password_error(self):
    #     #获取验证码
    #     response=self.session.get(self.verify_url)
    #     print("type=",response.headers.get("Content-type"))
    #     self.assertIn("image",response.headers.get("Content-type"))
    #     self.assertEqual(200,response.status_code)
    #
    #     data = {"username": "jacke",
    #                 "password": "xjt345",
    #                 "verifyCode": "9999"}
    #     response=self.session.post(self.login_url,data=data)
    #     json_data=response.json()
    #     print("login response data1=",json_data)
    #     #断言
    #     self.assertEqual(200,response.status_code)
    #     self.assertEqual(5901,json_data.get("code"))
    #     self.assertEqual("用户名或密码输入有误",json_data.get("msg"))

#
# if __name__=='__main__':
#     unittest.main()
#     suite = unittest.TestSuite()
#     suite.addTest(TestLogin('test_login_success'))
#     suite.addTest(TestLogin('test_login_username_is_not_exsit'))
#     suite.addTest(TestLogin('test_login_pwd_is_error'))
#     runner = unittest.TextTestRunner()
#     runner.run(suite)
#     # 这里 addTest()方法没有起到作用，还是按照原来的顺序执行了。原因是：在pycharm中，引入了unittest 模块，会默认按照 unittest
#     # 模式执行。需要将unittest模式转换成普通模式。
#     # 创建TextTestRunner类的实例对象
#
#
#     # # 测试报告文件路径
#     # report_file = "./report/report{}.html".format(time.strftime("%Y%m%d-%H%M%S"))
#     # with open(report_file, "wb") as f:
#     #     runner = HTMLTestRunner(f, title="TPshop接口自动化测试报告", description="V1.0")
#     #     # 运行测试套件
#     #     runner.run(suite)